package net.cloudcodex.client.storyboard.player.presenter;

import java.util.List;

import net.cloudcodex.client.storyboard.player.PlayerBoardEventBus;
import net.cloudcodex.client.storyboard.player.view.MessageView;
import net.cloudcodex.client.storyboard.player.view.MessageViewImpl;
import net.cloudcodex.shared.MessageAction;
import net.cloudcodex.shared.MessageType;
import net.cloudcodex.shared.dto.campaign.character.CharacterHeaderDTO;
import net.cloudcodex.shared.dto.msg.MessageDTO;

import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

@Presenter(view = MessageViewImpl.class, multiple = true)
public class MessagePresenter extends BasePresenter<MessageView, PlayerBoardEventBus> {
	private ScenePresenter parent;
	private MessageDTO message;

	public void mergeMessage(MessageDTO messageToMerge) {
		final MessageAction actionToMerge = messageToMerge.getAction();
		if (this.message == null) {
			// Init
			this.message = messageToMerge;
			// Author
			final Long authorId = messageToMerge.getAuthor();
			if (authorId != null) {
				final CharacterHeaderDTO character = this.parent.getCharacter(authorId);
				this.view.setCharacter(character.getName(), character.getIcon(),character.getId());
			}
			// Type
			final MessageType type = message.getType();
			if (MessageType.ACTION.equals(type)) {
				final MessageAction action = message.getAction();
				if (action != null) {
					this.view.setAction(actionToMerge.toLocaleString());
				}
			} else if (MessageType.DICEROLL.equals(type)) {
				updateDice(type);
			}
			// Content
			this.view.setContent("(" + messageToMerge.getId() + ")(" + messageToMerge.getIndex() + ") "
					+ messageToMerge.getContent());
		} else {
			// Update
			final long indexToMerge = messageToMerge.getIndex();
			if (message.getIndex() != indexToMerge) {
				// Message need to be moved by its scene
				this.parent.moveMessage((int) message.getIndex(), (int) indexToMerge);
				this.view.setContent("(" + messageToMerge.getId() + ")(" + messageToMerge.getIndex() + ") "
						+ message.getContent());
			}
			final String contentToMerge = messageToMerge.getContent();
			if (!message.getContent().equals(contentToMerge)) {
				message.setContent(contentToMerge);
				this.view.setContent("(" + messageToMerge.getId() + ")(" + messageToMerge.getIndex() + ") "
						+ contentToMerge);
			}
			final MessageType type = message.getType();
			if (MessageType.ACTION.equals(type)) {
				final MessageAction action = message.getAction();
				if (action != null && !action.equals(actionToMerge)) {
					message.setAction(actionToMerge);
					this.view.setAction(actionToMerge.toLocaleString());
				}
			} else if (MessageType.DICEROLL.equals(type)) {
				updateDice(type);
			}
		}
	}

	public ScenePresenter getParent() {
		return this.parent;
	}

	public void setParent(ScenePresenter parent) {
		this.parent = parent;
	}

	private void updateDice(final MessageType type) {
		final List<String> dies = message.getDices();
		final StringBuilder sb = new StringBuilder();
		for (String die : dies) {
			if (sb.length() != 0) {
				sb.append(" - ");
			}
			sb.append(die);
		}
		this.view.setAction(sb.toString());
	}
}
